package thread;


public class WaitAndNotifyTest {

    public static void main(String[] args) throws InterruptedException {
        Print print = new Print();
        Thread threadzhangsan = new Thread(new MyThreadZhangSan(print), "张三");
        threadzhangsan.setPriority(10);
        Thread threadLiSI = new Thread(new MyThreadLiSi(print), "李四");
        threadLiSI.setPriority(2);
        Thread threadWangWu = new Thread(new MyThreadWangWu(print), "王五");
        threadWangWu.setPriority(5);
        threadzhangsan.start();
        threadWangWu.start();
        Thread.sleep(1000);
        threadLiSI.start();

    }

}
class Print {
    public synchronized void method1() throws InterruptedException {
        for (int i = 1;i<=100;i++){
            System.out.println(Thread.currentThread().getName()+":打印"+i);
            if (i==50){
                this.wait();
            }
        }
    }

    /**
     * 留给李四打印的
     */
    public synchronized  void mehtod2(){
        for (int i = 1;i<=100;i++){
            System.out.println(Thread.currentThread().getName()+":打印"+i);
            if (i==50){
                this.notifyAll();
            }
        }
    }

}

class MyThreadZhangSan implements Runnable{

    private Print print;

    public MyThreadZhangSan(Print print) {
        this.print = print;
    }

    @Override
    public void run() {
        try {
            print.method1();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class MyThreadLiSi implements Runnable{

    private Print print;

    public MyThreadLiSi(Print print) {
        this.print = print;
    }

    @Override
    public void run() {
        print.mehtod2();
    }
}

class MyThreadWangWu implements Runnable{

    private Print print;

    public MyThreadWangWu(Print print) {
        this.print = print;
    }

    @Override
    public void run() {
        try {
            print.method1();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

